// pages/service/service.js
var url_js = require("../../utils/url.js");
var user_js = require("../../utils/user.js");
var common_js = require("../../utils/common.js");
var ubt_js = require("../../utils/ubt.js");
var name_js = require("../../utils/ubtButtonName.js");
var api = require("../../utils/apiRequest.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    sysInfo: null,
    contentStatus: -1, //1有数据
    serviceList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //业务埋点
    ubt_js.myPageView(common_js.currentPageName(), options);

    //1.禁止转发
    wx.hideShareMenu();
    //2.初始化数据
    this.initData(options);
    //3.请求数据
    this.requestToGetCustomerList();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    // 业务埋点
    ubt_js.myPageViewOut(common_js.currentPageName());
  },


  //初始化数据
  initData: function() {
    let tempUserInfo = user_js.userInfo();
    this.setData({
      userInfo: tempUserInfo
    });
  },

  //请求数据
  requestToGetCustomerList: function() {
    let data = {
      cityId: this.data.userInfo.ztd.cityId,
      code: user_js.getCode(),
      shopId: this.data.userInfo.ztd.id
    }
    api.wxRequest(url_js.customerListUrl, data).then(res => {
      this.handleCustomerList(res);
    }).catch(err => {
      common_js.log(err);
    });
  },

  //处理返回的客服结果 
  handleCustomerList: function(res) {
    var tmpList = res.result;
    if (tmpList.length > 0) {
      this.setData({
        contentStatus: 1,
        serviceList: tmpList
      })
    }
  },

  //复制
  onCopy: function(e) {
    var index = e.currentTarget.dataset.index;
    var item = this.data.serviceList[index];
    if (!item.wechatNo) {
      common_js.showToast('微信号不存在');
      return;
    }

    //设置剪切板
    wx.setClipboardData({
      data: item.wechatNo,
      success: res => {
        common_js.showToast('微信号已复制');
      }
    })
  }
})